﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Littlebeer.ConnectionStringHelper.OleDB.Excel
{
    public class OleDBExcelConnection
    {
        public static string Get(string path)
        {
            string result = "";
            switch (GetExt(path).ToLower())
            {
                case ("xls"):
                    {
                        result = Getxls(path);
                        break;
                    }
                case ("xlsx"):
                    {
                        result = Getxlsx(path);
                        break;
                    }
                default:
                    {
                        throw new Exception("Invalid file Extension");
                    }
            }
            return result;
        }
        static string Getxls(string path)
        {
            string result = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";", path);
            return result;
        }

        static string Getxlsx(string path)
        {
            string result = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", path);
            return result;
        }
        private static string GetExt(string path)
        {
            string result = path.Substring(path.LastIndexOf(".") + 1, (path.Length - path.LastIndexOf(".") - 1));
            return result;
        }
    }
}
